package com.right.handler;

import com.right.command.CsCommand;
import com.right.db.Bicycle;
import com.right.db.BicycleLocation;
import com.right.db.BicycleTrace;
import com.right.factory.NodeStatus;
import com.right.mapper.*;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <pre>
 *     @author : pillar
 *     e-mail : 347637454@qq.com
 *     time   : 2018/12/19 17:44
 *     desc   :
 *     version: 1.0
 * </pre>
 */
@Slf4j
public class LocationHandler implements PackageHandler {

    private final BicycleTemporarinessMapper bicycleTemporarinessMapper;

    private final BicycleMapper bicycleMapper;

    private final BicycleTraceMapper bicycleTraceMapper;

    private final BicycleLocationMapper bicycleLocationMapper;

    public LocationHandler(BicycleTemporarinessMapper bicycleTemporarinessMapper,
                           BicycleMapper bicycleMapper,
                           BicycleTraceMapper bicycleTraceMapper,
                           BicycleLocationMapper bicycleLocationMapper) {
        this.bicycleTemporarinessMapper = bicycleTemporarinessMapper;
        this.bicycleMapper = bicycleMapper;
        this.bicycleTraceMapper = bicycleTraceMapper;
        this.bicycleLocationMapper = bicycleLocationMapper;
    }

    @Override
    public boolean applies(List<String> dataList) {
        return CsCommand.LOCATION.equals(dataList.get(0));
    }

    @Override
    public boolean handleMessage(ChannelHandlerContext ctx, List<String> dataList) {
        String lockNo = ctx.channel().attr(NodeStatus.LOCK_NO).get();
        log.info("位置信息={}", lockNo);

        // 判断是否定位 V 未定位
        String hasLocation = dataList.get(2);

        if (!"V".equals(hasLocation)) {
            // 成功定位



            Example example = new Example(Bicycle.class);
            example.createCriteria().andEqualTo("lockNo", lockNo);

            Bicycle bicycle = bicycleMapper.selectOneByExample(example);

            if (bicycle == null) {
                log.error("锁编号：" + lockNo + " 未查找到车辆信息");
                return false;
            }

            // 插入轨迹记录
            BicycleTrace bicycleTrace = new BicycleTrace();
            bicycleTrace.setBicycle_id(bicycle.getId());
            bicycleTrace.setLock_id(bicycle.getLockId());
            bicycleTrace.setGpsLongitude(Double.valueOf(dataList.get(2)));
            bicycleTrace.setGpsLatitude(Double.valueOf(dataList.get(3)));
            bicycleTrace.setSample_time(new Date());

            int count = bicycleTraceMapper.insertSelective(bicycleTrace);

            if (count > 0) {
                // 更改车辆经纬度
                bicycle.setGpsLongitude((int) (Double.parseDouble(dataList.get(2)) * 1000000));
                bicycle.setGpsLatitude((int) (Double.parseDouble(dataList.get(3)) * 1000000));

                bicycleMapper.updateByPrimaryKey(bicycle);


                // 插入车辆位置

                BicycleLocation bicycleLocation = new BicycleLocation();
                bicycleLocation.setCreated_at(new Date());
                bicycleLocation.setGpsLongitude(Double.valueOf(dataList.get(2)));
                bicycleLocation.setGpsLatitude(Double.valueOf(dataList.get(3)));
                bicycleLocation.setLock_id(bicycle.getLockId());
                bicycleLocation.setStatus(hasLocation.equals("A") ? 1 : -1);
                bicycleLocationMapper.insertSelective(bicycleLocation);
            } else {
                log.error("锁编号：" + lockNo + " 插入轨迹失败");
            }
        }

        return false;
    }
}
